<template>
  <div>
    <!-- 添加招标公告 2018/3/6 17:54 zhangQ -->
    <Row>
      <i-col span="24">
        <div style="" class="doc-header">
          <Form :model="addBiddingForm" :label-width="120" ref="addBiddingForm" :rules="addBiddingRule">
            <FormItem label="项目名称" prop="projectName">
              <Input v-model="addBiddingForm.projectName" placeholder="项目名称"></Input>
            </FormItem>
            <FormItem label="发布单位" prop="publicCompany">
              <Input v-model="addBiddingForm.publicCompany" placeholder="发布单位"></Input>
            </FormItem>

            <FormItem label="招标截止时间">
              <DatePicker v-model="addBiddingForm.endTime" type="datetime" placeholder="Select date and time" style="width: 200px"></DatePicker>
            </FormItem>

            <FormItem label="公告正文" prop="content">
              <Input v-model="addBiddingForm.content" type="textarea" :rows="6" placeholder="公告正文"></Input>
            </FormItem>

            <FormItem label="公告区域">
              <Cascader :data="cityList" change-on-select v-model="addBiddingForm.bulletinArea"></Cascader>
            </FormItem>

            <FormItem label="联系人" prop="people">
              <Input v-model="addBiddingForm.people" placeholder="联系人"></Input>
            </FormItem>

            <FormItem label="联系电话" prop="telephone">
              <Input v-model="addBiddingForm.telephone" placeholder="联系电话"></Input>
            </FormItem>

            <FormItem label="联系邮箱" prop="email">
              <Input v-model="addBiddingForm.email" placeholder="联系邮箱"></Input>
            </FormItem>

            <FormItem>
              <Button type="primary" @click="submitAdd('addBiddingForm')">提交</Button>
              <Button type="ghost" style="margin-left: 8px" @click="goBack">取消</Button>
            </FormItem>
          </Form>
        </div>
      </i-col>
    </Row>
  </div>
</template>
<script>
  import { addBidding, getCityList } from '@api/backEnd/productDemand'
  import moment from 'moment'

  export default {
    name: 'billingAdd',
    data () {
      return {
        addBiddingForm: {
          projectName: '',
          publicCompany: '',
          endTime: null,
          content: null,
          bulletinArea: [],
          people: '',
          telephone: null,
          email: null
        },
        cityList: [],
        addBiddingRule: {
          projectName: [
            { required: true, message: '项目名称必填', trigger: 'blur' }
          ],
          publicCompany: [
            { required: true, message: '发布单位必填', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '公告正文必填', trigger: 'blur' }
          ],
          telephone: [
            { required: true, message: '联系电话必填', trigger: 'blur' },
            { pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/, message: '号码格式不正确！', trigger: 'blur' }
          ],
          people: [
            { required: true, message: '联系人必填', trigger: 'blur' }
          ],
          email: [
            { type: 'email', message: '邮箱格式不正确！', trigger: 'blur' }
          ]
        }
      }
    },
    computed: {},
    methods: {
      goBack () {
        this.$router.go(-1)
      },
      submitAdd (name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            if (!this.addBiddingForm.endTime || this.addBiddingForm.endTime < new Date()) {
              this.$Message.error('招标截止时间有误！')
              return false
            }
            const projectName = this.addBiddingForm.projectName
            const publicCompany = this.addBiddingForm.publicCompany
            const endTime = moment(this.addBiddingForm.endTime).format('YYYY-MM-DD HH:mm:ss')
            const content = this.addBiddingForm.content
            console.log(this.addBiddingForm.bulletinArea)
            const bulletinArea = this.addBiddingForm.bulletinArea[this.addBiddingForm.bulletinArea.length - 1]
            const people = this.addBiddingForm.people
            const telephone = this.addBiddingForm.telephone
            const email = this.addBiddingForm.email
            const param = { projectName, publicCompany, endTime, content, bulletinArea, people, telephone, email }
            addBidding(param).then(res => {
              if (+res.data.isSuccess === 1) {
                this.$Message.info('发布成功')
                this.$router.push({ path: 'billing_list' })
              } else {
                this.$Message.error('发布失败，请重试！')
              }
            })
          }
        })
      },
      setInitData () {
        getCityList().then(res => {
          if (res.data.isSuccess === 1) {
            this.cityList = res.data.data
          }
        })
      }
    },
    mounted () {
      this.setInitData()
    },
    components: {}
  }
</script>
<style scoped>

</style>
